#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
MySQL MCP Server 安装配置文件

这个文件定义了项目的元数据、依赖关系和安装配置。
支持通过 pip 安装和分发。
"""

import os
import sys
from pathlib import Path
from setuptools import setup, find_packages

# 确保 Python 版本兼容性
if sys.version_info < (3, 9):
    raise RuntimeError("MySQL MCP Server 需要 Python 3.9 或更高版本")

# 获取项目根目录
HERE = Path(__file__).parent.absolute()

# 读取 README 文件作为长描述
def read_readme():
    """读取 README.md 文件内容"""
    readme_path = HERE / "README.md"
    if readme_path.exists():
        with open(readme_path, "r", encoding="utf-8") as f:
            return f.read()
    return "MySQL MCP Server - 高性能的 MySQL 数据库 MCP 协议服务器"

# 读取版本信息
def get_version():
    """从版本文件中读取版本号"""
    version_file = HERE / "src" / "mysql_mcp_server" / "__version__.py"
    if version_file.exists():
        with open(version_file, "r", encoding="utf-8") as f:
            version_globals = {}
            exec(f.read(), version_globals)
            return version_globals["__version__"]
    return "0.1.0"

# 读取依赖列表
def read_requirements(filename="requirements.txt"):
    """读取依赖文件"""
    req_path = HERE / filename
    if req_path.exists():
        with open(req_path, "r", encoding="utf-8") as f:
            return [line.strip() for line in f if line.strip() and not line.startswith("#")]
    return []

# 基础依赖
base_requirements = [
    "mysql-connector-python>=8.0.0",
    "asyncio-mqtt>=0.11.0",
    "pydantic>=2.0.0",
    "python-dotenv>=1.0.0",
    "structlog>=23.0.0",
    "click>=8.0.0",
    "uvloop>=0.17.0; sys_platform != 'win32'",
    "colorama>=0.4.0",
]

# 开发依赖
dev_requirements = [
    "pytest>=7.0.0",
    "pytest-asyncio>=0.21.0",
    "pytest-cov>=4.0.0",
    "pytest-mock>=3.10.0",
    "black>=23.0.0",
    "flake8>=6.0.0",
    "mypy>=1.0.0",
    "isort>=5.12.0",
    "pre-commit>=3.0.0",
]

# 测试依赖
test_requirements = [
    "pytest>=7.0.0",
    "pytest-asyncio>=0.21.0",
    "pytest-cov>=4.0.0",
    "pytest-mock>=3.10.0",
    "testcontainers[mysql]>=3.7.0",
]

# 文档依赖
docs_requirements = [
    "sphinx>=6.0.0",
    "sphinx-rtd-theme>=1.2.0",
    "sphinx-autodoc-typehints>=1.22.0",
    "myst-parser>=1.0.0",
]

# 性能分析依赖
profile_requirements = [
    "py-spy>=0.3.0",
    "memory-profiler>=0.60.0",
    "line-profiler>=4.0.0",
]

# 安全扫描依赖
security_requirements = [
    "bandit>=1.7.0",
    "safety>=2.3.0",
]

setup(
    # 基本信息
    name="mysql-mcp-server",
    version=get_version(),
    description="高性能的 MySQL 数据库 MCP 协议服务器",
    long_description=read_readme(),
    long_description_content_type="text/markdown",
    
    # 作者信息
    author="Your Name",
    author_email="your.email@example.com",
    maintainer="Your Name",
    maintainer_email="your.email@example.com",
    
    # 项目链接
    url="https://github.com/yourusername/mysql-mcp-server",
    project_urls={
        "Bug Reports": "https://github.com/yourusername/mysql-mcp-server/issues",
        "Source": "https://github.com/yourusername/mysql-mcp-server",
        "Documentation": "https://mysql-mcp-server.readthedocs.io/",
        "Changelog": "https://github.com/yourusername/mysql-mcp-server/blob/main/CHANGELOG.md",
    },
    
    # 许可证
    license="MIT",
    
    # 分类信息
    classifiers=[
        "Development Status :: 4 - Beta",
        "Intended Audience :: Developers",
        "Intended Audience :: System Administrators",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.9",
        "Programming Language :: Python :: 3.10",
        "Programming Language :: Python :: 3.11",
        "Programming Language :: Python :: 3.12",
        "Programming Language :: Python :: Implementation :: CPython",
        "Topic :: Database",
        "Topic :: Database :: Database Engines/Servers",
        "Topic :: Internet :: WWW/HTTP :: HTTP Servers",
        "Topic :: Software Development :: Libraries :: Python Modules",
        "Topic :: System :: Networking",
        "Typing :: Typed",
    ],
    
    # 关键词
    keywords=[
        "mysql", "mcp", "server", "database", "protocol",
        "async", "connection-pool", "sql", "query-engine"
    ],
    
    # Python 版本要求
    python_requires=">=3.9",
    
    # 包配置
    packages=find_packages(where="src"),
    package_dir={"": "src"},
    
    # 包含非 Python 文件
    include_package_data=True,
    package_data={
        "mysql_mcp_server": [
            "py.typed",
            "*.json",
            "*.yaml",
            "*.yml",
        ],
    },
    
    # 依赖配置
    install_requires=base_requirements,
    extras_require={
        "dev": dev_requirements,
        "test": test_requirements,
        "docs": docs_requirements,
        "profile": profile_requirements,
        "security": security_requirements,
        "all": (
            dev_requirements +
            test_requirements +
            docs_requirements +
            profile_requirements +
            security_requirements
        ),
    },
    
    # 命令行工具
    entry_points={
        "console_scripts": [
            "mysql-mcp-server=mysql_mcp_server.main:main",
            "mcp-mysql=mysql_mcp_server.main:main",
        ],
    },
    
    # 数据文件
    # data_files=[
    #     ("share/mysql-mcp-server/examples", [
    #         "examples/config.env.example",
    #         "examples/docker-compose.example.yml",
    #     ]),
    #     ("share/mysql-mcp-server/docs", [
    #         "README.md",
    #         "LICENSE",
    #     ]),
    # ],
    
    # 压缩安全
    zip_safe=False,
    
    # 平台特定配置
    platforms=["any"],
    
    # 测试配置
    # test_suite="tests",
    # tests_require=test_requirements,
    
    # 项目状态
    # 可选值: "Planning", "Pre-Alpha", "Alpha", "Beta", "Production/Stable", "Mature", "Inactive"
    # 对应 classifiers 中的 "Development Status"
)

# 安装后的验证
if __name__ == "__main__":
    print("MySQL MCP Server 安装配置")
    print(f"版本: {get_version()}")
    print(f"Python 要求: {sys.version_info}")
    print("依赖包:")
    for req in base_requirements:
        print(f"  - {req}")